// view/index/addyuan/addyuan.js
let app = getApp()
let yuming = app.globalData.yuming
let mobile = app.globalData.mobile
let sfzza = app.globalData.sfzza
var chaninese = app.globalData.chaninese
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    buttonClicked: false,
    s2: false,
    yuming: yuming,
    heights: "",
    title: '添加员工',
    xingming: '', //姓名
    dianhua: '', //电话
    shenfenz: '', //身份证号
    zhiweiid: '', //职位id
    qutime: '', //取证时间
    daotime: '', //有效期至
    zhiguo: '请选择职位',
    // zhilist: [{ name: '店长', id: 2}, {  name: '员工', id: 3 }], //职位
    zhiindex: 0, //职位index
    date: '请选择健康证取证时间', //开始
    dates: '请选择健康证取证时间', //结束
    jkz: '', //健康证照片
    cyz: '', //从业证图片
    wujkz: '',
    wucyz: '',
    cyzdatequ: '请选择从业取证时间',
    cyzdatedao: '请选择从业证有效期',
    jkzzjlx: '', //健康证证件类型
    cyzzjlx: '', //从业证证件类型
    fdaaa: '',
    fdimg: '',
    Position: '', //职位
    inputHeight: 0
  },
  showModal(e) {
    var aaa = e.currentTarget.dataset.aaa
    var img = e.currentTarget.dataset.img
    this.setData({
      fdaaa: aaa,
      fdimg: img
    })
    let {
      type
    } = e.currentTarget.dataset, va = '';
    if (type == 's2') {
      va = "s2";
    }
    this.setData({
      [va]: true
    })
  },
  confirmbtn(e) {
    console.log(e)
    console.log('点击了从新上传！')
    var aaa = this.data.fdaaa
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        if (aaa == 0) {
          that.setData({
            jkz: urls,
            wujkz: data.imgUrl
          });
        }
        if (aaa == 1) {
          that.setData({
            cyz: urls,
            wucyz: data.imgUrl
          });
        }
      })
    })

  },
  cancelBtn() {
    console.log('点击了放大！')
    wx.previewImage({
      urls: [this.data.fdimg]
    })
  },
  getIptValue(e) {
    console.log(e.detail)
  },
  // 保存验证
  yanz1() {

  },
  xubaocun() {
    if (this.data.xingming == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
    } else if (this.data.dianhua == '') {
      wx.showToast({
        title: '请输入联系电话',
        icon: 'none'
      })
    } else if (this.data.dianhua.length != 11) {
      wx.showToast({
        title: '请输入11位手机号',
        icon: 'none'
      })
    } else if (mobile.test(this.data.dianhua) == false) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
    } else if (this.data.shenfenz == '') {
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none'
      })
    } else if (this.data.shenfenz.length != 18) {
      wx.showToast({
        title: '请输入18位身份证号',
        icon: 'none'
      })
    } else if (sfzza.test(this.data.shenfenz) == false) {
      wx.showToast({
        title: '请输入有效身份证号',
        icon: 'none'
      })
    } else if (this.data.Position == '') {
      wx.showToast({
        title: '请输入职位',
        icon: 'none'
      })
    } else if ((chaninese.test(this.data.Position))) {
      wx.showToast({
        title: '只允许输入中文',
        icon: 'none'
      })
      this.setData({
        Position: ''
      })
    } else if (this.data.jkzzjlx != '' || this.data.date != '请选择健康证取证时间' || this.data.jkz != '') {
      if (this.data.jkzzjlx == '') {
        wx.showToast({
          title: '请输入健康证岗位信息',
          icon: 'none'
        })
      } else if (this.data.date == '请选择健康证取证时间') {
        wx.showToast({
          title: '请选择健康证取证时间',
          icon: 'none'
        })
        // } else if (this.data.dates == '请选择健康证有效期') {
        //   wx.showToast({
        //     title: '请选择健康证有效期',
        //     icon: 'none'
        //   })
      } else if (this.data.jkz == '' || this.data.jkz == null) {
        wx.showToast({
          title: '请上传健康证',
          icon: 'none'
        })
      } else {
        if (this.data.cyzzjlx != '' || this.data.cyzdatequ != '请选择从业取证时间' || this.data.cyzdatedao != '请选择从业证有效期' || this.data.cyz != '') {
          if (this.data.cyzzjlx == '') {
            wx.showToast({
              title: '请输入从业证岗位信息',
              icon: 'none'
            })
          } else if (this.data.cyzdatequ == '请选择从业取证时间') {
            wx.showToast({
              title: '请选择从业证取证时间',
              icon: 'none'
            })
          } else if (this.data.cyzdatedao == '请选择从业证有效期') {
            wx.showToast({
              title: '请选择从业证有效期',
              icon: 'none'
            })
          } else if (this.data.cyz == '' || this.data.cyz == null) {
            wx.showToast({
              title: '请上传从业证',
              icon: 'none'
            })
          } else {
            this.queding()
          }
        } else {
            this.queding()
        }
      }
    } else {
      this.queding()
    }
  },
  // 添加
  queding() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    let data = {};
    if ((that.data.jkzzjlx == '' || that.data.date == '请选择健康证取证时间' || that.data.wujkz == '') && (that.data.cyzzjlx == '' || that.data.cyzdatequ == '请选择从业取证时间' || that.data.cyzdatedao == '请选择从业证有效期' || that.data.wucyz == '')) {
      data = {
        shopId: wx.getStorageSync('shopId'),
        Name: that.data.xingming,
        Phone: that.data.dianhua,
        idNumber: that.data.shenfenz,
        Position: that.data.Position,
      }
    } else if (that.data.jkzzjlx == '' || that.data.date == '请选择健康证取证时间' || that.data.wujkz == '') {
      data = {
        shopId: wx.getStorageSync('shopId'),
        Name: that.data.xingming,
        Phone: that.data.dianhua,
        idNumber: that.data.shenfenz,
        Position: that.data.Position,
        practiceDcment: that.data.cyzzjlx,
        practicestarTime: that.data.cyzdatequ,
        practiceendTime: that.data.cyzdatedao,
        practiceImage: that.data.wucyz,
      }
    } else if (that.data.cyzzjlx == '' || that.data.cyzdatequ == '请选择从业取证时间' || that.data.cyzdatedao == '请选择从业证有效期' || that.data.wucyz == '') {
      data = {
        shopId: wx.getStorageSync('shopId'),
        Name: that.data.xingming,
        Phone: that.data.dianhua,
        idNumber: that.data.shenfenz,
        Position: that.data.Position,
        healthyDcment: that.data.jkzzjlx,
        obtainTime: that.data.date,
        effectiveTime: that.data.dates,
        healthyImage: that.data.wujkz,
      }
    } else {
      data = {
        shopId: wx.getStorageSync('shopId'),
        Name: that.data.xingming,
        Phone: that.data.dianhua,
        idNumber: that.data.shenfenz,
        Position: that.data.Position,
        healthyDcment: that.data.jkzzjlx,
        obtainTime: that.data.date,
        effectiveTime: that.data.dates,
        healthyImage: that.data.wujkz,
        practiceDcment: that.data.cyzzjlx,
        practicestarTime: that.data.cyzdatequ,
        practiceendTime: that.data.cyzdatedao,
        practiceImage: that.data.wucyz,
      }
    }
    this.myRequest(yuming + 'shop/shopAddStaff', data, 'POST').then(res => {
      console.log(res)
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0
          })
          wx.reLaunch({
            url: '/page_one/congye/congye',
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 职位
  Position(e) {
    if ((chaninese.test(e.detail.value))) {
      wx.showToast({
        title: '只允许输入中文',
        icon: 'none'
      })
      this.setData({
        Position: ''
      })
    } else {
      this.setData({
        Position: e.detail.value
      })
    }
  },
  // 输入健康证类型
  jkzzjlx(e) {
    if ((chaninese.test(e.detail.value))) {
      wx.showToast({
        title: '只允许输入中文',
        icon: 'none'
      })
      this.setData({
        jkzzjlx: ''
      })
    } else {
      this.setData({
        jkzzjlx: e.detail.value
      })
    }
  },
  // 输入从业证类型
  cyzzjlx(e) {
    if ((chaninese.test(e.detail.value))) {
      wx.showToast({
        title: '只允许输入中文',
        icon: 'none'
      })
      this.setData({
        cyzzjlx: ''
      })
    } else {
      this.setData({
        cyzzjlx: e.detail.value
      })
    }
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa //店头营业执照
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        if (aaa == 0) {
          that.setData({
            jkz: urls,
            wujkz: data.imgUrl
          });
        }
        if (aaa == 1) {
          that.setData({
            cyz: urls,
            wucyz: data.imgUrl
          });
        }
      })
    })
  },
  // 职位
  zhiwei(e) {
    let index = e.detail.value
    this.setData({
      zhiguo: this.data.zhilist[e.detail.value].name,
      zhiweiid: this.data.zhilist[e.detail.value].id
    });
  },
  // 填写姓名
  xingming(e) {
    this.setData({
      xingming: e.detail.value
    })
  },
  // 填写电话
  dianhua(e) {
    this.setData({
      dianhua: e.detail.value
    })
  },
  // 填写身份证
  shenfenz(e) {
    this.setData({
      shenfenz: e.detail.value
    })
  },
  // 健康证选择开始时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
    let date = new Date(this.data.date);
    let year = date.getFullYear() + 1
    let month = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1);
    let day = (date.getDate() < 10 ? "0" + (date.getDate()) : date.getDate());
    this.setData({
      dates: year + '-' + month + '-' + day
    })
  },
  // 健康证选择结束时间
  // bindDateChanges: function (e) {
  //   if (new Date(e.detail.value).getTime() < Date.parse(new Date())) {
  //     wx.showToast({
  //       title: '有效期不可小于当前时间',
  //       icon: 'none'
  //     })
  //     this.setData({
  //       dates: '请选择健康证有效期'
  //     })
  //   } else {
  //     this.setData({
  //       dates: e.detail.value
  //     })
  //   }
  // },
  // 从业证选择开始时间
  bindcyzqu: function (e) {
    this.setData({
      cyzdatequ: e.detail.value,
    })
  },
  // 从业证选择结束时间
  bindcyzdao: function (e) {
    if (new Date(e.detail.value).getTime() < Date.parse(new Date())) {
      wx.showToast({
        title: '有效期不可小于当前时间',
        icon: 'none'
      })
      this.setData({
        cyzdatedao: '请选择从业证有效期'
      })
    } else {
      this.setData({
        cyzdatedao: e.detail.value
      })
    }
  },
  sheng() {
    wx.navigateTo({
      url: '/my_other/xinxilr/xinxilr',
    })
  },
  inputFocus(e) {
    console.log(e, '弹起');
    console.log(e.detail.height);
    this.setData({
      inputHeight: e.detail.height * 2
    })
    // setTimeout(() => {
    wx.pageScrollTo({
      scrollTop: 999999,
      duration: 0
    });
    // }, 200)
  },
  inputBlur() {
    this.setData({
      inputHeight: 0
    })
  },
  backs() {
    // wx.navigateBack({
    //   delta: 0
    // })
    wx.reLaunch({
      url: '/page_one/congye/congye',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
    })
    if (wx.getStorageSync('token') == "") {
      wx.showModal({
        title: '需要授权哦~',
        cancelText: '取消',
        confirmText: '确定',
        success: function (res) {
          if (res.confirm) {
            console.log("确定")
            wx.navigateTo({
              url: '/page_zhu/shou/shou?sta=0&url=page_one/addyuan/addyuan',
            })
          } else {
            console.log("取消")
            wx.navigateBack({
              delta: 0,
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})